"""系统托盘模块"""

from PySide6.QtWidgets import QSystemTrayIcon, QMenu, QApplication
from PySide6.QtCore import QObject, Signal, Qt
from PySide6.QtGui import QIcon, QPixmap, QPainter, QFont, QAction

from .database import ClipboardDatabase
from .clipboard_monitor import ClipboardUtils


class SystemTray(QObject):
    """系统托盘管理器"""
    
    # 信号定义
    show_window = Signal()
    quit_application = Signal()
    # 移除 toggle_topmost 信号
    
    def __init__(self, database: ClipboardDatabase):
        super().__init__()
        self.database = database
        self.tray_icon = None
        # 移除 is_topmost 属性
        self.create_tray_icon()
    
    def create_tray_icon(self):
        """创建系统托盘图标"""
        if not QSystemTrayIcon.isSystemTrayAvailable():
            return False
        
        # 创建图标
        icon = self.create_icon()
        
        # 创建托盘图标
        self.tray_icon = QSystemTrayIcon(icon)
        self.tray_icon.setToolTip("智能剪切板")
        
        # 创建右键菜单
        self.create_context_menu()
        
        # 连接信号
        self.tray_icon.activated.connect(self.on_tray_activated)
        
        return True
    
    def create_icon(self) -> QIcon:
        """创建托盘图标"""
        # 创建一个简单的剪切板图标
        pixmap = QPixmap(32, 32)
        pixmap.fill(Qt.transparent)
        
        painter = QPainter(pixmap)
        painter.setRenderHint(QPainter.Antialiasing)
        
        # 绘制剪切板背景
        painter.setBrush(Qt.white)
        painter.setPen(Qt.black)
        painter.drawRoundedRect(4, 2, 24, 28, 2, 2)
        
        # 绘制夹子
        painter.setBrush(Qt.gray)
        painter.drawRoundedRect(12, 0, 8, 6, 1, 1)
        
        # 绘制文本线条
        painter.setPen(Qt.blue)
        painter.drawLine(8, 12, 24, 12)
        painter.drawLine(8, 16, 20, 16)
        painter.drawLine(8, 20, 22, 20)
        
        painter.end()
        
        return QIcon(pixmap)
    
    def create_context_menu(self):
        """创建右键菜单"""
        menu = QMenu()
        
        # 显示主窗口
        show_action = QAction("显示主窗口", self)
        show_action.triggered.connect(self.show_window.emit)
        menu.addAction(show_action)
        
        # 移除置顶相关菜单项
        
        menu.addSeparator()
        
        # 统计信息
        stats = self.database.get_stats()
        stats_action = QAction(f"总计: {stats['total_items']} 项", self)
        stats_action.setEnabled(False)
        menu.addAction(stats_action)
        
        menu.addSeparator()
        
        # 退出
        quit_action = QAction("退出", self)
        quit_action.triggered.connect(self.quit_application.emit)
        menu.addAction(quit_action)
        
        self.tray_icon.setContextMenu(menu)
    
    # 移除 on_toggle_topmost 方法
    
    # 移除 set_topmost_state 方法

    def on_tray_activated(self, reason):
        """托盘图标激活处理"""
        if reason == QSystemTrayIcon.DoubleClick:
            self.show_window.emit()
        elif reason == QSystemTrayIcon.Trigger:
            # 单击时更新菜单
            self.update_context_menu()
    
    def update_context_menu(self):
        """更新右键菜单"""
        self.create_context_menu()
    
    def show_message(self, title: str, message: str, icon=QSystemTrayIcon.Information, timeout=3000):
        """显示系统通知"""
        if self.tray_icon and self.tray_icon.isVisible():
            self.tray_icon.showMessage(title, message, icon, timeout)
    
    def show(self):
        """显示托盘图标"""
        if self.tray_icon:
            self.tray_icon.show()
    
    def hide(self):
        """隐藏托盘图标"""
        if self.tray_icon:
            self.tray_icon.hide()
    
    def is_visible(self) -> bool:
        """检查托盘图标是否可见"""
        return self.tray_icon and self.tray_icon.isVisible()
    
    def set_tooltip(self, tooltip: str):
        """设置提示文本"""
        if self.tray_icon:
            self.tray_icon.setToolTip(tooltip) 